<!--
 * @Author: ChaiHongJun
 * @Date: 2021-07-08 09:13:58
 * @LastEditors: ChaiHongJun
 * @LastEditTime: 2021-07-15 15:40:07
 * @version: 
 * @Description: 
-->
<template>
  <div class="my-container">
    <van-cell-group class="my-info" v-if="$store.state.user.token">
      <van-cell center class="base-info">
        <van-image
          class="avatar"
          slot="icon"
          width="50"
          height="50"
          round
          fit="cover"
          :src="currentUser.photo"
        />
        <template #title>
          <div class="name">{{ currentUser.name }}</div>
        </template>
        <van-button size="small" round>编辑资料</van-button>
      </van-cell>
      <van-grid class="data-info">
        <van-grid-item class="data-info-item">
          <template #text>
            <div class="text-wrap">
              <div class="count">{{ currentUser.art_count }}</div>
              <div class="text">头条</div>
            </div>
          </template>
        </van-grid-item>
        <van-grid-item class="data-info-item">
          <template #text>
            <div class="text-wrap">
              <div class="count">{{ currentUser.follow_count }}</div>
              <div class="text">关注</div>
            </div>
          </template>
        </van-grid-item>
        <van-grid-item class="data-info-item">
          <template #text>
            <div class="text-wrap">
              <div class="count">{{ currentUser.fans_count }}</div>
              <div class="text">粉丝</div>
            </div>
          </template>
        </van-grid-item>
        <van-grid-item class="data-info-item">
          <template #text>
            <div class="text-wrap">
              <div class="count">{{ currentUser.like_count }}</div>
              <div class="text">获赞</div>
            </div>
          </template>
        </van-grid-item>
      </van-grid>
    </van-cell-group>
    <div class="not-login" v-else>
      <div @click="$router.push('/login')"></div>
      <div class="text">
        <van-button
          type="info"
          @click="$router.push({ name: 'Login', path: '/login' })"
          >登录/注册</van-button
        >
      </div>
    </div>
    <van-grid :column-num="2">
      <van-grid-item icon-prefix="toutiao" icon="shoucang" text="收藏" />
      <van-grid-item icon-prefix="toutiao" icon="lishi" text="历史" />
    </van-grid>
    <van-cell title="消息通知" is-link to="/" v-if="$store.state.user.token" />
    <van-cell title="小智同学" is-link to="/" v-if="$store.state.user.token" />
    <van-cell title="退出登录" @click="logout" v-if="$store.state.user.token" />
  </div>
</template>
<script>
import { getUserInfo } from "@/api/user";
import { Toast, Dialog } from "vant";
export default {
  name: "My",
  data() {
    return {
      currentUser: {}, // 当前用户
    };
  },
  components: {},
  mounted() {
    // 获取用户信息
    if (this.$store.state.user.token) {
      this.getUserInfo();
    }
  },
  methods: {
    async getUserInfo() {
      try {
        let { status, data } = await getUserInfo(this.$store.state.user.token);
        if (status == 200) {
          console.log("获取信息成功");
          this.currentUser = data.data;
        } else if (status == 400) {
          Toast.fail({
            duration: 3000,
            message: "请求参数错误",
            position: top,
          });
        } else if (status == 401) {
          Toast.fail({
            duration: 3000,
            message: "用户认证失败",
            position: top,
          });
        } else if (status == 507) {
          Toast.fail({
            duration: 3000,
            message: "数据库错误",
            position: top,
          });
        }
      } catch (err) {
        //未知错误
        Toast.fail({ duration: 3000, message: err.message, position: top });
        console.log(err);
      }
    },
    logout() {
      //弹窗确认是否退出
      Dialog.confirm({
        title: "退出提示",
        message: "您确定要退出么？",
      })
        .then(() => {
          // on confirm
          this.$store.commit("user/SET_USER", {
            token: null,
            refresh_token: null,
          });
          Toast.success({ message: "退出成功", position: top });
          //注销后回到首页
          // this.$router.push({ path: "/", name: "Home" });
        })
        .catch(() => {
          // on cancel
        });
    },
  },
};
</script>
<style scoped lang="less">
.my-container {
  .my-info {
    background: url("");
    background-size: cover;
    .base-info {
      box-sizing: border-box;
      height: 115px;
      background-color: unset;
      padding-top: 38px;
      padding-bottom: 11px;
      .avatar {
        box-sizing: border-box;
        width: 66px;
        height: 66px;
        border: 1px solid #fff;
        margin-right: 11px;
      }
      .name {
        font-size: 15px;
        // color: #fff;
      }
      .update-btn {
        height: 16px;
        font-size: 10px;
        color: #666666;
      }
    }
    .data-info {
      .data-info-item {
        height: 65px;
        // color: #fff;
        .text-wrap {
          display: flex;
          flex-direction: column;
          justify-content: center;
          align-items: center;
          .count {
            font-size: 18px;
          }
          .text {
            font-size: 11px;
          }
        }
      }
    }
    /deep/ .van-grid-item__content {
      background-color: unset;
    }
  }
  .not-login {
    height: 180px;
    // background: url("./banner.png") no-repeat;
    background-size: cover;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    .mobile {
      width: 66px;
      height: 66px;
    }
    .text {
      font-size: 14px;
      // color: #fff;
    }
  }
  /deep/ .nav-grid {
    .nav-grid-item {
      height: 70px;
      .toutiao {
        font-size: 22px;
      }
      .toutiao-shoucang {
        color: #eb5253;
      }
      .toutiao-lishi {
        color: #ff9d1d;
      }
      .van-grid-item__text {
        font-size: 14px;
        color: #333333;
      }
    }
  }
  .logout-cell {
    text-align: center;
    color: #d86262;
  }
  .mb-4 {
    margin-bottom: 4px;
  }
}
</style>
